# -*- coding: utf-8 -*-
import time
import json

from amac_spider.items import PofSecuritiesItem
from amac_spider.utils.my_redis_spider import RedisSpiderPro
from amac_spider.utils.handler import get_url, get_datetime


# 证券公司集合资管产品公示
class PofSecuritiesSpider(RedisSpiderPro):
    name = 'pof_securities'

    def parse(self, response):
        item = PofSecuritiesItem()
        mappings = {'product_id': 'id', 'product_name': 'cpmc', 'product_code': 'cpbm', 'manager': 'gljg',
                    'trustee': 'tgjg', 'record_time': 'barq', 'found_time': 'slrq', 'expire_time': 'dqr',
                    'investment_stat': 'tzlx', 'divide_level': 'sffj', 'work_stat': 'yzzt'}
        info_list = json.loads(response.text)['content']
        for info in info_list:
            for k, v in mappings.items():
                item[k] = info[v]
            item['detail_url'] = get_url(self.name) + '/detail.html?id={}'.format(item['product_id'])
            item['crawl_time'] = get_datetime(time.time())
            yield item
